package com.chen.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.internalcommon.constant.CommonStatusEnum;
import com.chen.internalcommon.constant.DriverCarConstants;
import com.chen.internalcommon.dto.DriverCarBindingRelationship;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class DriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;


    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        log.info("{}", driverCarBindingRelationship);
        // 车辆和司机是一一对应的
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId())
                .or()
                .eq("car_id", driverCarBindingRelationship.getCarId());
        DriverCarBindingRelationship selectOne = driverCarBindingRelationshipMapper.selectOne(queryWrapper);
        if (selectOne == null) {
            driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
            driverCarBindingRelationship.setBindingTime(LocalDateTime.now());
            driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
            log.info("司机{}车辆{}绑定成功", driverCarBindingRelationship.getDriverId(), driverCarBindingRelationship.getCarId());
            return ResponseResult.success();
        }
        Long selectOneCarId = selectOne.getCarId();
        Long selectOneDriverId = selectOne.getDriverId();
        Long carId = driverCarBindingRelationship.getCarId();
        Long driverId = driverCarBindingRelationship.getDriverId();
        if (Objects.equals(selectOneCarId, carId) && Objects.equals(selectOneDriverId, driverId)) {
            if (selectOne.getBindState() == DriverCarConstants.DRIVER_CAR_BIND) {
                log.info("司机{}和车辆{}已绑定,请勿重复绑定", driverId, carId);
                return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS);
            } else {
                selectOne.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
                selectOne.setUnBindingTime(LocalDateTime.now());
                driverCarBindingRelationshipMapper.updateById(selectOne);
                log.info("司机{}和车辆{}绑定成功", driverId, carId);
                return ResponseResult.success();
            }
        } else if (Objects.equals(selectOneCarId, carId)) {
            if (selectOne.getBindState() == DriverCarConstants.DRIVER_CAR_BIND) {
                log.info("车辆{}已被其他司机绑定,请勿重复绑定", carId);
                return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS);
            } else {
                selectOne.setDriverId(driverId);
                selectOne.setBindingTime(LocalDateTime.now());
                selectOne.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
                driverCarBindingRelationshipMapper.updateById(selectOne);
                log.info("司机{}和车辆{}绑定成功", driverId, carId);
                return ResponseResult.success();
            }
        } else {
            if (selectOne.getBindState() == DriverCarConstants.DRIVER_CAR_BIND) {
                log.info("司机{}已绑定其他车辆，请勿重复绑定", driverId);
                return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS);
            } else {
                selectOne.setCarId(carId);
                selectOne.setBindingTime(LocalDateTime.now());
                selectOne.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
                driverCarBindingRelationshipMapper.updateById(selectOne);
                log.info("司机{}和车辆{}绑定成功", driverId, carId);
                return ResponseResult.success();
            }
        }

    }

    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId())
                .eq("car_id", driverCarBindingRelationship.getCarId())
                .eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship selectOne = driverCarBindingRelationshipMapper.selectOne(queryWrapper);
        if (selectOne != null) {
            selectOne.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);
            selectOne.setUnBindingTime(LocalDateTime.now());
            driverCarBindingRelationshipMapper.updateById(selectOne);
            log.info("司机{}和车辆{}解绑完成", selectOne.getDriverId(), selectOne.getCarId());
            return ResponseResult.success();
        }
        log.info("司机{}和车辆{}绑定关系不存在", driverCarBindingRelationship.getDriverId(), driverCarBindingRelationship.getCarId());
        return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS);
    }
}
